//
//  PPPViewController.m
//  HappyStore
//
//  Created by pzx on 16/5/9.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "PPPViewController.h"
#import "projectLibraryVC.h"
#import "policyCell.h"
#import "newsDetailVC.h"
#import "goveNewsVC.h"
#import "informationVC.h"


NSString *policycell = @"policyCell";
@interface PPPViewController ()<ImagePlayerViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (strong, nonatomic) NSMutableArray *modelArr; // 新闻数组
@property (strong, nonatomic) NSMutableDictionary *guowuyuanDict;// 国务院文件
@property (strong, nonatomic) NSMutableDictionary *falvfaguiDict;// 法律法规文件
@property (strong, nonatomic) NSMutableDictionary *buweiDict;// 部委文件
@property (strong, nonatomic) NSMutableDictionary *difangDict;// 地方文件
@property (strong, nonatomic) NSMutableArray * wenJianArr;
@property (strong, nonatomic) NSArray *imgArr; // 轮播图组
@property (strong, nonatomic) NSArray *btnArr; // 中段按钮组
@property (strong, nonatomic) UITableView *newsTableView;// 新闻列表
@property (strong, nonatomic) UITableView *policyTableView;// 文件列表
@property (strong, nonatomic) UIView *topLineView;
@property (strong, nonatomic) UIView *lastLineView;
@property (nonatomic, strong) ImagePlayerView *imagePlayerView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (strong, nonatomic) UILabel *linLabel;
@end

@implementation PPPViewController
#pragma mark - 初始化
- (ImagePlayerView *)imagePlayerView
{
    if (!_imagePlayerView) {
        self.imagePlayerView = [[ImagePlayerView alloc] init];
    }
    return _imagePlayerView;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] init];
    }
    return _scrollView;
}
-(NSMutableArray *)modelArr{
    if (!_modelArr) {
        _modelArr = [NSMutableArray new];
    }
    return _modelArr;
}
-(NSMutableDictionary *)falvfaguiDict{
    if (!_falvfaguiDict) {
        _falvfaguiDict = [NSMutableDictionary new];
    }
    return _falvfaguiDict;
}
-(NSMutableDictionary *)buweiDict{
    if (!_buweiDict) {
        _buweiDict = [NSMutableDictionary new];
    }
    return _buweiDict;
}
-(NSMutableDictionary *)difangDict{
    if (!_difangDict) {
        _difangDict = [NSMutableDictionary new];
    }
    return _buweiDict;
}
-(NSMutableDictionary *)guowuyuanDict{
    if (!_guowuyuanDict) {
        _guowuyuanDict = [NSMutableDictionary new];
    }
    return _guowuyuanDict;
}
-(NSMutableArray *)wenJianArr{
    if (!_wenJianArr) {
        _wenJianArr = [NSMutableArray array];
    }
    return _wenJianArr;
}
-(NSArray *)btnArr{
    if (!_btnArr) {
        _btnArr = [NSArray array];
    }
    return _btnArr;
}

-(NSArray *)imgArr{
    if (!_imgArr) {
        _imgArr = [NSArray new];
    }
    return _imgArr;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.f],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    
    
}
-(void)leftBarButtonItemClick{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];

//    [self.view addSubview:progressView];
//    [progressView show:YES];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"PPP";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    // 请求数据
    NSDictionary * dic = [NSDictionary dictionary];
    [LSHttpTool sendHttpData:@"pppHomeRecord_app_ppp.im" params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        NSDictionary *dataDic = [NSDictionary dictionary];
        dataDic = dicInfo[@"data"];
        self.imgArr = dataDic[@"homePhotos"];

       self.modelArr = dataDic[@"pppNewS"];

        self.guowuyuanDict = dataDic[@"pppPolicyGuowuyuan"];
        [self.wenJianArr addObject:self.guowuyuanDict];
        
         self.falvfaguiDict = dataDic[@"pppPolicyFalvfagui"];
        [self.wenJianArr addObject:self.falvfaguiDict];
        
         self.buweiDict = dataDic[@"pppPolicyBuwei"];
        [self.wenJianArr addObject:self.buweiDict];
        
        self.difangDict = dataDic[@"pppPolicyDifang"];
        [self.wenJianArr addObject:self.difangDict];
       
        
        // 设置轮播图
        [self setupAdRollView:self.imgArr];
        
        // 设置scrollview
        [self setupScrollView];
        
        // 设置中段按钮
        [self addBtn];
        
        // 设置TableView
        [self setupTableView];

        //设置返回按钮
        UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
        self.navigationItem.leftBarButtonItem = leftItem;
        
        [self hideBubble];

        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];

    }];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}
- (void)setupScrollView
{
    self.scrollView.frame = self.backView.bounds;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.scrollView];
}
#pragma mark - 设置中段按钮
-(void)addBtn{
    UIButton *btnO = [[UIButton alloc]initWithFrame:CGRectMake(15, self.backView.height/3 + 10, self.backView.width/3.5, self.backView.height/5)];
    [btnO setImage:[UIImage imageNamed:@"组-12"] forState:0];
    [self.scrollView addSubview:btnO];
    [btnO addTarget:self action:@selector(goProjectLibraryVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnS = [[UIButton alloc]initWithFrame:CGRectMake(btnO.x + btnO.width + 10, self.backView.height/3 + 10, self.backView.width/3.5, self.backView.height/10 - 5)];
    [btnS setImage:[UIImage imageNamed:@"组-13"] forState:0];
    [btnS addTarget:self action:@selector(gogoveNewsVC) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btnS];
    
    UIButton *btnT = [[UIButton alloc]initWithFrame:CGRectMake(btnS.x + btnS.width + 10, self.backView.height/3 + 10, self.backView.width/3.5, self.backView.height/10 - 5)];
    [btnT setImage:[UIImage imageNamed:@"组-15"] forState:0];
    [btnT addTarget:self action:@selector(goSet) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btnT];
    
    UIButton *btnF = [[UIButton alloc]initWithFrame:CGRectMake(btnS.x, btnS.y + btnS.height+ 10, self.backView.width/3.5, self.backView.height/10 - 5)];
    [btnF setImage:[UIImage imageNamed:@"组-14"] forState:0];
    [btnF addTarget:self action:@selector(goSetSec) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btnF];
    
    UIButton *btnL = [[UIButton alloc]initWithFrame:CGRectMake(btnT.x, btnT.y + btnT.height+ 10, self.backView.width/3.5, self.backView.height/10 - 5)];
    [btnL setImage:[UIImage imageNamed:@"组-16"] forState:0];
    [btnL addTarget:self action:@selector(goSetThird) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:btnL];
    
    // 上分割线
    UIView *topLineView = [[UIView alloc]initWithFrame:CGRectMake(0, btnO.y + btnO.height + 10, self.backView.width, 10)];
    topLineView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    self.topLineView = topLineView;
    [self.scrollView addSubview:topLineView];
}

#pragma mark - 跳转
-(void)goSet{
    informationVC * information = [[informationVC alloc]init];
    information.titleStr = @"PPP资讯";
    [self.navigationController pushViewController:information animated:YES];
}
-(void)goSetSec{
    informationVC * information = [[informationVC alloc]init];
    information.titleStr = @"成功案例";
    [self.navigationController pushViewController:information animated:YES];
}
-(void)goSetThird{
    goveNewsVC * goveZhengCe = [[goveNewsVC alloc]init];
    goveZhengCe.titleStr = @"合同范本";
    [self.navigationController pushViewController:goveZhengCe animated:YES];
}

-(void)goProjectLibraryVC{
    projectLibraryVC * projectVC = [[projectLibraryVC alloc] init];
    projectVC.tigStr = @"ppp";
    [self.navigationController pushViewController:projectVC animated:YES];
}
-(void)gogoveNewsVC{
    goveNewsVC * goveZhengCe = [[goveNewsVC alloc]init];
    goveZhengCe.titleStr = @"政策法规";
    [self.navigationController pushViewController:goveZhengCe animated:YES];
}
-(void)setupTableView{
    UITableView *newsTableView = [[UITableView alloc] init];
    newsTableView.x = 0;
    newsTableView.y = self.topLineView.y + self.topLineView.height;
    newsTableView.height = self.modelArr.count * 50 + 20;
    newsTableView.width = self.backView.width;
    newsTableView.delegate = self;
    newsTableView.dataSource = self;
    [newsTableView setScrollEnabled:NO];
    self.newsTableView = newsTableView;
    [self.scrollView addSubview:self.newsTableView];
    
    // 下分割线
    UIView *lastLineView = [[UIView alloc]initWithFrame:CGRectMake(0, newsTableView.y + newsTableView.height + 10, self.backView.width, 10)];
    lastLineView.backgroundColor = [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
    self.lastLineView = lastLineView;
    [self.scrollView addSubview:lastLineView];
    
    UITableView *policyTableView = [[UITableView alloc] init];
    policyTableView.x = 0;
    policyTableView.y = self.lastLineView.y + self.lastLineView.height;
//    policyTableView.height = self.otherModelArr.count * 80 + 20;
    policyTableView.height = 440;
    policyTableView.width = self.backView.width;
    policyTableView.delegate = self;
    policyTableView.dataSource = self;
    [policyTableView setScrollEnabled:NO];
    self.policyTableView = policyTableView;
    [self.scrollView addSubview:self.policyTableView];
    
    self.scrollView.contentSize = CGSizeMake(0, self.imagePlayerView.height + self.backView.height/5 + 20 + self.newsTableView.height + self.lastLineView.height + self.policyTableView.height);
    
}
#pragma mark - 轮播代理
/**
 *  ad广告图片轮播
 *
 */
- (void)setupAdRollView:(NSArray *)pictureArray
{
    self.imagePlayerView.imagePlayerViewDelegate = self;

    self.imagePlayerView.scrollInterval = 5.0f;

    self.imagePlayerView.pageControlPosition = ICPageControlPosition_BottomRight;

    self.imagePlayerView.hidePageControl = NO;
    
    if (pictureArray.count == 1) {
        self.imagePlayerView.pageControl.hidden = YES;
    } else self.imagePlayerView.pageControl.hidden = NO;
    
    self.imagePlayerView.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    
    self.imagePlayerView.x = 0;
    self.imagePlayerView.y = 0;
    self.imagePlayerView.width = self.backView.width;
    self.imagePlayerView.height = self.backView.height/3;
    self.imagePlayerView.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:self.imagePlayerView];
    
    [self.imagePlayerView reloadData];
}
#pragma mark - ImagePlayerViewDelegate

- (NSInteger)numberOfItems
{
    return self.imgArr.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    NSString *urlStr = self.imgArr[index][@"urlPhoto"];
    [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"release_logo"]];

}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    //NSLog(@"did tap index = %d", (int)index);
    
    
}

#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:self.modelArr.count];
    
    if ((tableView = self.newsTableView)) {
        
        return self.modelArr.count;
        
    }else if (tableView == self.policyTableView){
        return 4;
    }
    else return 0;
    ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView == self.newsTableView) {
        
        UITableViewCell *newsCell = [self.newsTableView dequeueReusableCellWithIdentifier:@"newsCell"];
        if (!newsCell) {
            newsCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        NSDictionary *dataDict = self.modelArr[indexPath.row];
        
//        NSLog(@"%@",dataDict[@"content"]);
        UIImageView *newsImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 40, 20)];
        
        if ([dataDict[@"type"] isEqualToString:@"要闻"]) {
            newsImgView.image = [UIImage imageNamed:@"组-17"];
            [newsCell addSubview:newsImgView];
        }
        else if ([dataDict[@"type"] isEqualToString:@"知识"]){
            newsImgView.image = [UIImage imageNamed:@"组-19"];
            [newsCell addSubview:newsImgView];
        }
        else if ([dataDict[@"type"] isEqualToString:@"地方"]){
            newsImgView.image = [UIImage imageNamed:@"组-18"];
            [newsCell addSubview:newsImgView];
        }
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(newsImgView.x + newsImgView.width + 5, 10, self.backView.width - newsImgView.width - 15, 30)];
        title.textColor = [UIColor blackColor];
        title.font = [UIFont systemFontOfSize:12];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = dataDict[@"title"];
        
        newsCell.selectionStyle = UITableViewCellSelectionStyleNone;
        newsCell.accessoryType = UITableViewCellAccessoryNone;
        
        [newsCell addSubview:title];
        
        return newsCell;
    }else if (tableView == self.policyTableView){
        
        UINib * nib = [UINib nibWithNibName:@"policyCell" bundle:nil];
        [self.policyTableView registerNib:nib forCellReuseIdentifier:policycell];
        
        policyCell * cell = [self.policyTableView dequeueReusableCellWithIdentifier:policycell];
        if (!cell) {
            cell = [[policyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:policycell];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        NSDictionary *dataDict = self.wenJianArr[indexPath.row];
        
        // toptitle
        UILabel *linLabel = [[UILabel alloc]init];
        linLabel.text = dataDict[@"title"];
        linLabel.numberOfLines = 0;
        linLabel.textColor = [UIColor colorWithRed:42.0/255.0 green:123.0/255.0 blue:179.0/255.0 alpha:1];
        linLabel.textAlignment = NSTextAlignmentLeft;
        UIFont *font = [UIFont fontWithName:@"Arial" size:14];
        linLabel.font = font;
        CGSize constraint = CGSizeMake(self.backView.width - cell.mainImg.width - 15, 2000.0f);
        CGSize size = [linLabel.text sizeWithFont:font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
        [linLabel setFrame:CGRectMake(80, 13, size.width, size.height)];
        self.linLabel = linLabel;
        
        // 发文字号
        UILabel *fawenLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, self.linLabel.height + 15, 60, 17)];
        fawenLabel.textColor = [UIColor blackColor];
        fawenLabel.font = [UIFont systemFontOfSize:12];
        fawenLabel.text = @"发文字号";
        
        UILabel *fawen = [[UILabel alloc]initWithFrame:CGRectMake(145, self.linLabel.height + 15, self.backView.width/2, 17)];
        fawen.textColor = [UIColor blackColor];
        fawen.font = [UIFont systemFontOfSize:12];
        fawen.text = dataDict[@"fawenzihao"];
        
        // 发布日期
        UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, fawenLabel.y + fawenLabel.height + 13, 60, 17)];
        timeLabel.textColor = [UIColor blackColor];
        timeLabel.font = [UIFont systemFontOfSize:12];
        timeLabel.text = @"发布日期";
        
        UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(145, fawenLabel.y + fawenLabel.height + 13, self.backView.width/2, 17)];
        time.textColor = [UIColor blackColor];
        time.font = [UIFont systemFontOfSize:12];
        time.text = dataDict[@"chengwenTime"];
        
        [cell addSubview:self.linLabel];
        [cell addSubview:fawenLabel];
        [cell addSubview:fawen];
        [cell addSubview:time];
        [cell addSubview:timeLabel];

        
        NSArray * imgArr = @[@"组-21",@"组-22",@"部委规范文件",@"组-24"];
        cell.mainImg.image = [UIImage imageNamed:imgArr[indexPath.row]];
        
        return cell;
    }

    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.newsTableView) {
        NSDictionary * dict = self.modelArr[indexPath.row];
        newsDetailVC * newsVC = [[newsDetailVC alloc]init];
        newsVC.titleStr = dict[@"newsFrom"];
        newsVC.urlStr = dict[@"content"];
        [self.navigationController pushViewController:newsVC animated:YES];
    }else{
        NSDictionary * dict = self.wenJianArr[indexPath.row];
        newsDetailVC * newsVC = [[newsDetailVC alloc]init];
        newsVC.titleStr = dict[@"title"];
        newsVC.urlStr = dict[@"content"];
        [self.navigationController pushViewController:newsVC animated:YES];
    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.newsTableView) {
        
        return 50;
        
    }else if(tableView == self.policyTableView){
        policyCell *cell = [self tableView:self.policyTableView cellForRowAtIndexPath:indexPath];
        //设置cell自适应高度
        CGRect cellFrame = [cell frame];
        cellFrame.origin = CGPointMake(0, 0);
        CGFloat totalHeight = self.linLabel.height + 80;
        cellFrame.size.height = totalHeight;
        [cell setFrame:cellFrame];
        
        self.policyTableView.height = cell.height *4 + 20;
        self.scrollView.contentSize = CGSizeMake(0, self.imagePlayerView.height + self.backView.height/5 + 20 + self.newsTableView.height + self.lastLineView.height + self.policyTableView.height + 30);
        
        return cell.frame.size.height ;
    }
    
    else return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.backView.width, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 20, 20)];
    imgview.image = [UIImage imageNamed:@"resizeApi-(1)"];
    [view addSubview:imgview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(imgview.x + imgview.width + 10, 5, 70, 20)];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:14];
    
    if (tableView == self.newsTableView) {
        
        label.text = @"新闻动态";
        [view addSubview:label];
        
    }else if(tableView == self.policyTableView){
        label.text = @"政策法规";
        [view addSubview:label];
    }

    return view;
}
- (void)dealloc{
    
    self.imagePlayerView.imagePlayerViewDelegate = nil;
}

@end
